<template>
  <div id="app">
    <router-view/>
    <!-- alert弹窗 -->
    <mask-alert />
  </div>
</template>

<script>
import maskAlert from './components/layout/alert';
export default {
  name: 'App',
  data() {
    return {
      num: 0,
    }
  },
  components: {
    maskAlert
  },
  mounted() {
    var that = this;
    document.addEventListener('plusready', function() {
      that.checkUpdate();
      var webview = plus.webview.currentWebview();
      plus.key.addEventListener('backbutton', function() {
        webview.canBack(function(e) {
          if(e.canBack && that.$route.path != '/login') {
            that.num = 0;
            if(that.onLogin) {
              that.setShow();
            }else {
              webview.back();
            }
          }else{
            if(that.num == 1){
              webview.close();
            }else{
              that.num++;
              that.$toast({
                message: '再按一次确认退出',
                duration: 600
              });
            }
          }
        })
      });
    });
  },
  methods:{
    checkUpdate(){
      var clientType = 0;
      var _  = this;
      clientType=window.plus.os.name=='Android'?1:2;
      plus.runtime.getProperty(plus.runtime.appid,function(inf){
        var checkUrl = 'http://oss.hmboss.com/api.php?c=checkup';
        _.$axios({method: 'post', url: checkUrl, data: {id: "1911061",sn:inf.version,tp:clientType}}).then(function (data) { //客户编号 版本号 IOS或安卓
          if (data.data.status == 1) {
            plus.nativeUI.showWaiting("检测到更新");
            var newVersion = data.data.version;
            if(newVersion.p_type==0){
              _.install(newVersion.p_file);
            }else{
              if(clientType==1){
                _.install(newVersion.p_file);
              }else{
                plus.runtime.openURL(data.data.downurl);
              }
            }
          } else {
          }
        });
      });
    },
    install(wgtUrl){
      plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
        if ( status == 200 ) {
          plus.runtime.install(d.filename,{force:true},function(){
            plus.nativeUI.closeWaiting();
            plus.nativeUI.alert("应用资源更新完成！",function(){
              plus.runtime.restart();
            });
          },function(e){
            plus.nativeUI.closeWaiting();
            console.log("安装wgt文件失败["+e.code+"]："+e.message);
          });
        } else {
          console.log("下载wgt失败！");
        }
        plus.nativeUI.closeWaiting();
      }).start();
    },
  }

}
</script>

<style lang="less">
  @import "./common/css/reset.less";
  #app {
    height: 100%;
    width: 100%;
    overflow-y: auto;
    font-family: 'Avenir', Helvetica, Arial,'微软雅黑', sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    color: #333;
  }
</style>
